import java.io.*;
import java.util.ArrayList;

/**
 * 
 * Read and writes a training file
 * 
 * @author tomalteman
 *
 */
public class TFile {
	
	private static int debug=0; 
	
	/**
	 * 
	 * Opens a file and reads each line into an ArrayList
	 * 
	 * @param file
	 * @return
	 */
	public static ArrayList<String> read(File file)
	{
		ArrayList<String> lines = new ArrayList<String>();
		String line="";
		
		try{
			BufferedReader in = new BufferedReader(new FileReader(file));
			
			// Read lines of input
			while((line = in.readLine())!= null)
			{
				lines.add(line);		
			}            
			in.close();
		} catch (FileNotFoundException fnfe){
			System.err.println("File Not Found: " + fnfe);
		} catch (IOException ioe){
			System.err.println("Invalid Input: " + ioe);
		} catch (Exception e){
			System.err.println("Other Error: " + e);
		}
		
		
		return lines;
	}
	
	public static void write(File file, ArrayList<String> list)
	{
		try{
			BufferedWriter wr = new BufferedWriter(new FileWriter(file));
			
			for(int i=0;i<list.size();i++)
			{
				wr.write(list.get(i));
				wr.newLine();
			}
			
			wr.close();
		} catch (FileNotFoundException fnfe){
			System.err.println("File Not Found: " + fnfe);
		} catch (IOException ioe){
			System.err.println("Invalid Input: " + ioe);
		} catch (Exception e){
			System.err.println("Other Error: " + e);
		}
	}
	
	/**
	 * Removes erroneous items from a file
	 * 
	 * Removes:
	 * 	& - '38'
	 * 
	 * @param file
	 */
	public static void correctFile(File file)
	{
		ArrayList<String> lines = read(file);
		ArrayList<String> newlines = new ArrayList<String>();
		String line="";
		char[] data;
		int c;
		
		for(int i =0;i<lines.size();i++) 
			{
				line = lines.get(i);
				if(debug==1) System.out.println("1: "+lines.get(i));
				
				if(line.startsWith("<t"))
				{
					data = line.toCharArray();
					for(int j=0;j<data.length;j++)
					{
						c = data[j];
						if(c == 38) 
							{
								data[j] = 32;
							}
					}
					line = (new String(data));
				}
				newlines.add(line);
				if(debug==1) System.out.println("2: "+newlines.get(i));
			}
		
		write(file,newlines);
	}
	
	/**
	 * Converts a file into a string
	 * @param file
	 * @return
	 */
	public static String toString(File file)
	{
		ArrayList<String> lines = read(file);
		String s ="";
		
		for(int i=0;i<lines.size();i++) s= s + " "+lines.get(i);
		
		
		return s;
	}
	
	/**
	 * Write a file to a training set given article with title, link, topic, text and source
	 * Written in XML format
	 * @param file
	 * @param art
	 */
	public static void writeArt(File file, article art)
	{
		try{
			BufferedWriter wr = new BufferedWriter(new FileWriter(file));
			
			wr.write("<item>");
			wr.newLine();
			
			//Write Title
			wr.write("<title>"+art.getTitle()+"</title>");
			wr.newLine();
			
			//Write link
			wr.write("<link>"+art.getLink()+"</link>");
			wr.newLine();
			
			//Write Category
			wr.write("<category>"+art.category +"</category>");
			
			//Write source
			wr.write("<source>"+art.getSource()+"</source>");
			wr.newLine();
			
			//Write topic
			wr.write("<topic>"+art.getTopic()+"</topic>");
			wr.newLine();
			
			//Write text
			wr.write("<text>"+art.getText()+"</text>");
			wr.newLine();
			
			wr.write("</item>");
			
			
			wr.close();
		} catch (FileNotFoundException fnfe){
			System.err.println("File Not Found: " + fnfe);
		} catch (IOException ioe){
			System.err.println("Invalid Input: " + ioe);
		} catch (Exception e){
			System.err.println("Other Error: " + e);
		}
	}

}
